/* api */
import { getProductQualityInfoRecordList } from '@src/api/ProductV2Api'
/* entity */
import { QualityInfoRecord } from '@model/entity/QualityInfo'
/* hooks */
import useFetch from '@hooks/useFetch'
/* model */
import { QualityInfoRecordResult } from '@model/param/out/Product'
import { getQualityInfoRecordSearchModel } from '@model/param/in/Product'
import Page from '@model/Page.ts'
/* vue */
import { computed, ComputedRef, ref, Ref } from 'vue'

export type FetchFunction<ParamsType, ResultType> = (params: ParamsType) => [ComputedRef<ResultType>, ComputedRef<boolean>]

export function usePage<ParamsType, ResultType>(fetchFunction: FetchFunction<ParamsType, ResultType>, pageOptions: Page) {
  // 页面对象
  let page: Ref<Page | ResultType> = ref(new Page(pageOptions))
  // 加载状态
  let loading: Ref<boolean> = ref(false)
  
  function setLoadingValue(isLoading: boolean) {
    loading.value = isLoading
  }
  
  function setLoadingRef(isLoading: Ref<boolean>) {
    loading = isLoading
  }
  
  function initPage() {
    page = ref(new Page(pageOptions))
  }
  
  function loadmore(fetchParams: ParamsType) {
    setLoadingValue(true)
    const [data, fetchLoading] = fetchFunction(fetchParams)
    page = data
    setLoadingRef(fetchLoading)
  }
  
  function refresh(fetchParams: ParamsType) {
    initPage()
    setLoadingValue(true)
    const [data, fetchLoading] = fetchFunction(fetchParams)
    page = data
    setLoadingRef(fetchLoading)
  }
  
  return [page, loadmore, refresh]
}

export function useQualityInfoRecordFetch(params: getQualityInfoRecordSearchModel): [ComputedRef<QualityInfoRecordResult | null>, Ref<boolean>] {
  let [data, callback, loading] = useFetch<getQualityInfoRecordSearchModel, QualityInfoRecordResult>(
    async (getQualityRecordParams: getQualityInfoRecordSearchModel) => {
      return getProductQualityInfoRecordList(getQualityRecordParams)
    }, 
    params
  )
  
  callback()
  
  return (
    [
      computed(() => data.value.result),
      loading
    ]
  )
}

export function useQualityInfoRecord() {
  // 
}